#include "stdlib.h"
#include "string.h"
#include <hidef.h> /* for EnableInterrupts macro */
#include "Headers/derivative.h" /* include peripheral declarations */
#include "Headers/define.h"
#include "Headers/GPIO.h"
#include "Headers/SPI.h"
#include "Headers/ICG.h"
#include "Headers/micelaneous.h"
#include "Headers/RF.h"
#include "Headers/time.h"
#include "Headers/ADC.h"
#include "Headers/tinaco.h"
#include "Headers/regadera.h"
#include "Headers/232.h"
#include "Headers/receiver232.h"
//#define programatinaco  
#ifdef programatinaco
  #define programm tinaco();
  //#define programm empezar();
#else
  #define programm regadera();
  //#define programm receiver232()
#endif
 


word control_a, control_b, control_c, irq_status, rx_status, command_modem;
word version, gpio_data_in, gpio_data_in_e, gpio_dir;
char rx_pkt_latch, cca_final, adc_regadera = 0, adc_tinaco = 0, flag_tinaco = 0, flag_regadera = 0;
int time_out = 100, count_buzzer = 21;
void interrupt VectorNumber_Virq irqpin(void);

void main(void) {
char function = 0;
//word adc;
//char adc_n;
SetClock_FEI();
SPI_init(off);
setup_ATD();

	/* IRQ pin Interrup activation*/
  IRQSC = IRQSC_IRQPE_MASK | IRQSC_IRQACK_MASK | IRQSC_IRQIE_MASK;
  
  EnableInterrupts; /* enable interrupts */
  /* include your code here */
  GPIO_config('D',dos,output,on);
  GPIO_config('D',cuatro,output,on);
  GPIO_config('D',cinco,output,on);
  GPIO_config('D',seis,output,on);
  GPIO_config('D',siete,output,on);
  
  GPIO_config('A',dos,input,on);
  GPIO_config('A',tres,input,on);
  GPIO_config('A',cuatro,input,on);
  GPIO_config('A',cinco,input,on);
  
  
  delay_ms(10);
   
  SendCommand(PSM_Mode, 0xA0C0);
  SendCommand(Reserved, 0xFEC6);
  SendCommand(CLKO_Ctl, 0x7E00);
  SendCommand(PA_Lvl, 0x00FC); //Power adjustment output
  
  
  version = ReadCommand(Chip_Id);
  control_a = ReadCommand(Control_A);
  control_b = ReadCommand(Control_B);
  control_c = ReadCommand(Control_C);
  delay_us(1000);
  Set_up_pkt(15);//Acording to the protocol ZigBee Fc = 2405 + 5*(K-11), where 11 >= k <= 26
  
  setup_232(Baud9600, Normal_wire);
  for(;;){ __RESET_WATCHDOG();
    programm;
    //read_single_adc(5, &adc);
    //adc = adc >> 2;
    //adc_n = adc & 0x00ff;
    
  }
}

void interrupt VectorNumber_Virq irqpin(void){
	IRQSC |= IRQSC_IRQACK_MASK;   		// clear flag 
}